package henu.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import henu.bean.Lesson;
import henu.bean.Teacher;
import henu.factory.DaoFactory;

public class TeacherAction extends ActionSupport{
	private String orderField;  
	public String add(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String sn = request.getParameter("sn");
		String name = request.getParameter("name");
		String duty = request.getParameter("duty");
		String major = request.getParameter("major");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String qq = request.getParameter("qq");
		String memo = request.getParameter("memo");
		Teacher teacher = new Teacher();
		teacher.setfTeacherId(sn);
		teacher.setfTeachName(name);
		teacher.setfJobTitle(duty);
		teacher.setfDept(major);
		teacher.setfPhoneNumber(phone);
		teacher.setfEmail(email);
		teacher.setfQQ(qq);
		teacher.setfMemo(memo);
		int n = 0;
		n = DaoFactory.createTeacherDao().add(teacher);
		if(n == 1){
			return SUCCESS;		
		}else{
			return ERROR;
		} 		
	}
	
	public String getAll(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(); 
		List<Teacher> teachers = null;
		teachers = DaoFactory.createTeacherDao().getAll();
		
		if(teachers == null){
			return ERROR;
		}else{			
			session.setAttribute("teachers", teachers);
			return "getAll";
		}		
	}
	
	public String getAllByPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		System.out.println("page in");
		List<Teacher> teachers = null;
		int pageNow = Integer.parseInt(request.getParameter("num"));
		System.out.println("pageNow"+pageNow);
		teachers = DaoFactory.createTeacherDao().getAll(5,pageNow);
		System.out.println(teachers.size());
		int count = (int) DaoFactory.createTeacherDao().getCount();
		System.out.println(count);
		if(count%5==0){
			request.getSession().setAttribute("pageCount", count/5);
		}else{
			request.getSession().setAttribute("pageCount", (count/5)+1);
		}
		request.getSession().setAttribute("totalCount", count);
		request.getSession().setAttribute("teachers", teachers);
		request.getSession().setAttribute("pageNow", pageNow);
	
		return "getAll";
	}
	
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String fteacherId = request.getParameter("fteacherId");
		int n = DaoFactory.createTeacherDao().delete(fteacherId);
		if(n == 1){
			return "delete";
		}else{
			return ERROR;
		}
		
	}
	
	public String update(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(); 
		String fteacherId = request.getParameter("fteacherId");
		Teacher teacher = DaoFactory.createTeacherDao().get(fteacherId);
		if(teacher != null){
			session.setAttribute("teacher", teacher);
			return "update";
		}else{
			
			return ERROR;
		}
	}
	public String updateInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String sn = request.getParameter("sn");
		String name = request.getParameter("name");
		String duty = request.getParameter("duty");
		String major = request.getParameter("major");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String qq = request.getParameter("qq");
		String memo = request.getParameter("memo");
		Teacher teacher = new Teacher();
		teacher.setfTeacherId(sn);
		teacher.setfTeachName(name);
		teacher.setfJobTitle(duty);
		teacher.setfDept(major);
		teacher.setfPhoneNumber(phone);
		teacher.setfEmail(email);
		teacher.setfQQ(qq);
		teacher.setfMemo(memo);
		int n = 0;
		n = DaoFactory.createTeacherDao().update(teacher);
		System.out.println("n = "+n);
		if(n == 1){
			return "updateInfo";		
		}else{
			
			return ERROR;
		} 		
	}
	
	public String search(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(); 
		String method = request.getParameter("method");
		if(method.equals("id")){
			String fteacherId = request.getParameter("keyword");
			List<Teacher> teacher = DaoFactory.createTeacherDao().getById(fteacherId);
			session.setAttribute("teachers", teacher);
		}
		if(method.equals("name")){
			String name = request.getParameter("keyword");
			List<Teacher> teacher = DaoFactory.createTeacherDao().getByName(name);
			session.setAttribute("teachers", teacher);
		}
		if(method.equals("dept")){
			String dept = request.getParameter("keyword");
			List<Teacher> teacher = DaoFactory.createTeacherDao().getByDept(dept);
			session.setAttribute("teachers", teacher);
		}
		return "search";
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	
}




